package com.ptszyxx.popose.module.main.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YChatUtil;
import com.ptszyxx.popose.databinding.FragmentHomeCallMatchBinding;
import com.ysg.enums.ChatCallEnum;
import com.ptszyxx.popose.module.main.home.vm.HomeCallMatchVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStatusBarUtil;
import com.ysg.utils.YStringUtil;

/**
 * 语音速配；视频速配
 */
public class HomeCallMatchFragment extends BaseFragment<FragmentHomeCallMatchBinding, HomeCallMatchVM> {

    public static HomeCallMatchFragment getInstance() {
        return new HomeCallMatchFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home_call_match;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public HomeCallMatchVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(HomeCallMatchVM.class);
    }


    @Override
    public void initStatusBar() {
        super.initStatusBar();
        YStatusBarUtil.setColorTheme(this, binding.titleBar, com.ysg.R.color.transparent);
        YStatusBarUtil.setImageTheme(this, binding.titleBar);
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null){
            String callType = bundle.getString(BundleConstant.CHAT_CALL_TYPE);
            if (YStringUtil.isNotEmpty(callType)){
                viewModel.callEnum = ChatCallEnum.get(callType);
                YChatUtil.getInstance().setFreeType(viewModel.callEnum);
            }
        }
    }

    @Override
    public void initView() {
        super.initView();
        binding.titleBar.setTitle(viewModel.callEnum == ChatCallEnum.AUDIO? getResources().getString(R.string.home_call_match_audio_title): getResources().getString(R.string.home_call_match_video_title));
        binding.tvTip.setText(viewModel.callEnum == ChatCallEnum.AUDIO? getResources().getString(R.string.home_call_match_audio_tip): getResources().getString(R.string.home_call_match_video_tip));
        YImageUtil.show(binding.ivAvatar, YSPUtils.getInstance().getUserPic());
        YImageUtil.showGif(binding.ivGif, R.mipmap.ic_home_call_match);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestCallMatch("1");
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onCallEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String txUserId) {
                YActivityUtil.getInstance().jumpChatCall(txUserId, viewModel.callEnum);
            }
        });
    }
}
